package com.service.scheduling.task;


import com.alibaba.fastjson.JSON;
import com.service.dao.dto.HrComboOrder;
import com.service.dao.enums.SysMqTopicEnum;
import com.service.dao.model.SysMqRecord;
import com.service.scheduling.binding.OrderHrComboSendHandler;
import com.service.scheduling.service.SysMqRecordService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;

import java.util.List;

@Slf4j
@Component
@AllArgsConstructor
public class MqMsgRetryTask {

	private final SysMqRecordService sysMqRecordService;

	private final OrderHrComboSendHandler sendHandler;


//	@Scheduled(cron = "0 */3 * * * ?")
	public void doRun() {
		log.info("用户下单产品直推，业绩分红任务-开始");
		long startTime = System.currentTimeMillis();
		List<SysMqRecord> notReceive = sysMqRecordService.listNotReceiveMqMsg(SysMqTopicEnum.PAY_HR_COMBO_ORDER_TOPIC);
		if (ObjectUtils.isEmpty(notReceive)) {
			log.info("暂无未处理消息");
			return;
		}
		notReceive.forEach(item -> {
			if (StringUtils.isBlank(item.getSendData())) {
				return;
			}
			HrComboOrder takeOrder = JSON.parseObject(item.getSendData(), HrComboOrder.class);
			takeOrder.setMsgId(item.getRecordId());
			sendHandler.sendPayHrComboMsg(takeOrder);
		});
		long use = System.currentTimeMillis() - startTime;
		log.info("未处理消息补偿机制完成 耗时:{}", use / 1000);
		log.info("用户下单产品直推，业绩分红任务-退出");
	}
}
